package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.mapper.IntegralGradeMapper;
import com.atguigu.srb.core.service.IntegralGradeService;
import com.atguigu.srb.core.service.UserInfoService;
import com.atguigu.srb.result.ResponseEnum;
import com.atguigu.srb.util.JwtUtils;
import com.atguigu.srb.util.SrbAssert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 * 积分等级表 服务实现类
 * </p>
 *
 * @author Atguigu
 * @since 2024-04-15
 */
@Service
public class IntegralGradeServiceImpl extends ServiceImpl<IntegralGradeMapper, IntegralGrade> implements IntegralGradeService {
    @Resource
    private UserInfoService userInfoService;
    @Override
    public BigDecimal getBorrowAmountByToken(String token) {
        // 1. 根据token获取userID
        Long userId = JwtUtils.getUserId(token);
        // 2. 根据userId获取user对象，再获取user积分
        Integer integral = userInfoService.getById(userId).getIntegral();
        // 3. 根据user积分去积分等级表中获取积分等级对象，再获取借款额度
        IntegralGrade integralGrade = this.getOne(Wrappers.lambdaQuery(IntegralGrade.class)
                .le(IntegralGrade::getIntegralStart, integral)
                .ge(IntegralGrade::getIntegralEnd, integral));
        SrbAssert.assertTrue(integralGrade!=null, ResponseEnum.ERROR);
        return integralGrade.getBorrowAmount();
    }
}
